//
//  LoginViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/3.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class LoginViewController: UIViewController {
    
    var phoneImageView : UIImageView!
    var phoneTextField : UITextField!
    
    var bgView:UIView!

    var PasswordImageView : UIImageView!
    var PasswordTextField : UITextField!
    
    var bgView1:UIView!

    var verificationImageView : UIImageView!
    var verificationTextField : UITextField!
    
    var bgView2:UIView!
    
    
    var LoginButton : UIButton!
    
    var ForgetButton : UIButton!
    
    var codeImageView : UIImageView!
    
//    var codeView:CodeView!
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        

        
        
        self.view.backgroundColor = bgGrayColor;
        navigationItem.title="登录"
        
        //手机号

        phoneImageView = UIImageView(image: UIImage(named: "phone"))
        
        self.view.addSubview(phoneImageView)
        
        
        phoneTextField = UITextField()
        //设置边框样式为圆角矩形
        phoneTextField.borderStyle = UITextBorderStyle.none
        phoneTextField.placeholder = "手机号"
  
        self.view.addSubview(phoneTextField)
        
        
        
        bgView = UIView()
        bgView.backgroundColor = UIColor.gray
        self.view.addSubview(bgView)
        
        
        
        //密码
        
        
        PasswordImageView = UIImageView(image: UIImage(named: "Password"))

        self.view.addSubview(PasswordImageView)


        PasswordTextField = UITextField()
        //设置边框样式为圆角矩形
        PasswordTextField.borderStyle = UITextBorderStyle.none
        PasswordTextField.placeholder = "密码"
        PasswordTextField.isSecureTextEntry = true
        self.view.addSubview(PasswordTextField)
        
        bgView1 = UIView()
        bgView1.backgroundColor = UIColor.gray
        self.view.addSubview(bgView1)

        //验证码


        verificationImageView = UIImageView(image: UIImage(named: "verification"))

        self.view.addSubview(verificationImageView)


        verificationTextField = UITextField()
        //设置边框样式为圆角矩形
        verificationTextField.borderStyle = UITextBorderStyle.none
        verificationTextField.placeholder = "验证码"
        self.view.addSubview(verificationTextField)


        self.codeImageView = UIImageView()
        self.codeImageView.backgroundColor = UIColor.red

        Alamofire.request(URL.init(string: "http://wwwqjzsyzcscom.aykj.net.cn/api/validateCode")!).responseData { (dataResponse) in
            self.codeImageView.image = UIImage(data:  dataResponse.data!)
        }

        self.view.addSubview(self.codeImageView)
        


        
        bgView2 = UIView()
        bgView2.backgroundColor = UIColor.gray
        self.view.addSubview(bgView2)
        
        
        
        LoginButton = UIButton()
        //背景颜色
        LoginButton.backgroundColor = UIColor.blue
        LoginButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        LoginButton.setTitle("登录", for: .normal )
        LoginButton.layer.cornerRadius = 10
        LoginButton.layer.masksToBounds = true
        
        LoginButton.addTarget(self,action:#selector(LoginButtonClick), for: .touchUpInside)
        self.view.addSubview(LoginButton)
        
        
        
        ForgetButton = UIButton()
        //背景颜色
        
        ForgetButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        ForgetButton.setTitle("忘记密码?", for: .normal )
        ForgetButton.setTitleColor(UIColor.blue, for: .normal)
        
                ForgetButton.addTarget(self,action:#selector(ForgetButtonClick), for: .touchUpInside)
        self.view.addSubview(ForgetButton)
      
        
        self.layoutView()
        
  
        
    }
    

    
    //登录
    @objc func LoginButtonClick() {
        print("登录11111111")
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/login"
        let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
      
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
     
        Alamofire.request(request).responseJSON {
            (response) in
              print("====\(response)")
            if response.result.isSuccess {
                
                
                let json = JSON(response.data!)
                let model = LoginModel()
                model.account = json["account"].string
                model.name = json["name"].string
                model.token = json["token"].string
                
                
                
              
                
                let manager = UserDefaults()
                manager.setValue(model.account!, forKey:"accounts" )  //存储字符串
                manager.setValue(model.name!, forKey:"names" )
                manager.setValue(model.token!, forKey:"tokens" )

          
                let  MainVC = MainController()
                self.view.window?.rootViewController = MainVC
                
//                self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
                
            } else {
                
            }
        }
     


    }
    //忘记密码
    @objc func ForgetButtonClick() {
        print("忘记")
        
        let ForgetVC =  ForgetViewController()
        
        self.navigationController?.pushViewController(ForgetVC , animated: true)
    }
    
    func layoutView()  {
        
        
        self.phoneImageView.snp.makeConstraints { (make) in
            make.top.equalTo(self.view.snp.top).offset(110)
            make.left.equalTo(self.view.snp.left).offset(10)
            make.width.equalTo(20)
            make.height.equalTo(20)
        }
        
        self.phoneTextField.snp.makeConstraints { (make) in
            make.top.equalTo(self.view.snp.top).offset(100)
            make.left.equalTo(self.phoneImageView.snp.right).offset(5)
            make.right.equalTo(self.view.snp.right).offset(-10)
            make.height.equalTo(40)
        }
        
        self.bgView.snp.makeConstraints { (make) in
            make.top.equalTo(self.phoneTextField.snp.bottom).offset(5)
            make.left.equalTo(self.view.snp.left).offset(10)
            make.right.equalTo(self.view.snp.right).offset(-10)
            make.height.equalTo(1)
        }
        
        
        self.PasswordImageView.snp.makeConstraints { (make) in
            make.top.equalTo(self.bgView.snp.bottom).offset(15)
            make.left.equalTo(self.view.snp.left).offset(10)
            make.width.equalTo(20)
            make.height.equalTo(20)
        }
        
        self.PasswordTextField.snp.makeConstraints { (make) in
            make.top.equalTo(self.bgView.snp.bottom).offset(5)
            make.left.equalTo(self.PasswordImageView.snp.right).offset(5)
            make.right.equalTo(self.view.snp.right).offset(-10)
            make.height.equalTo(40)
        }
        
        self.bgView1.snp.makeConstraints { (make) in
            make.top.equalTo(self.PasswordTextField.snp.bottom).offset(5)
            make.left.equalTo(self.view.snp.left).offset(10)
            make.right.equalTo(self.view.snp.right).offset(-10)
            make.height.equalTo(1)
        }
        
        self.verificationImageView.snp.makeConstraints { (make) in
            make.top.equalTo(self.bgView1.snp.bottom).offset(15)
            make.left.equalTo(self.view.snp.left).offset(10)
            make.width.equalTo(20)
            make.height.equalTo(20)
        }
        
        self.verificationTextField.snp.makeConstraints { (make) in
            make.top.equalTo(self.bgView1.snp.bottom).offset(5)
            make.left.equalTo(self.verificationImageView.snp.right).offset(5)
            make.right.equalTo(self.view.snp.right).offset(-100)
            make.height.equalTo(40)
        }
        
        self.codeImageView.snp.makeConstraints { (make) in
            make.top.equalTo(self.bgView1.snp.bottom).offset(5)
          
            make.right.equalTo(self.view.snp.right).offset(-10)
            make.width.equalTo(80)
            make.height.equalTo(40)
        }
        
        self.bgView2.snp.makeConstraints { (make) in
            make.top.equalTo(self.verificationTextField.snp.bottom).offset(5)
            make.left.equalTo(self.view.snp.left).offset(10)
            make.right.equalTo(self.view.snp.right).offset(-10)
            make.height.equalTo(1)
        }
        
        self.LoginButton.snp.makeConstraints { (make) in
            make.top.equalTo(self.bgView2.snp.bottom).offset(20)
            make.left.equalTo(self.view.snp.left).offset(10)
            make.right.equalTo(self.view.snp.right).offset(-10)
            make.height.equalTo(40)
        }
        
        
        self.ForgetButton.snp.makeConstraints { (make) in
            make.top.equalTo(self.LoginButton.snp.bottom).offset(30)
            make.left.equalTo(self.view.snp.left).offset(10)
            make.right.equalTo(self.view.snp.right).offset(-10)
            make.height.equalTo(40)
        }
        
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
